package com.ama.controller;


import com.ama.dao.BuildDao;
import com.ama.pojo.Build;
import com.ama.pojo.Estate;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;

import static java.lang.Integer.parseInt;

@Controller
public class BuildManageController {
    @Autowired
    BuildDao buildDao;
    @RequestMapping("/addBuild")
    public String addBuild(HttpServletRequest request) {
        Long estateId = Long.valueOf(request.getParameter("estateId"));
        Long buildId = Long.valueOf(request.getParameter("buildId"));
        String name = request.getParameter("buildName");
        Integer buildFloornum = Integer.parseInt(request.getParameter("buildFloornum"));
        String buildAddress = request.getParameter("buildAddress");
        Double buildArea = Double.parseDouble(request.getParameter("buildArea"));
        Integer buildusertime = Integer.parseInt(request.getParameter("buildusetime"));
        Integer buildlasttime = Integer.parseInt(request.getParameter("buildlasttime"));
        Integer build_state = Integer.parseInt(request.getParameter("build_state"));
        Integer build_open = Integer.parseInt(request.getParameter("build_open"));
        String buildInfo = request.getParameter("buildInfo");
        String buildOwner = request.getParameter("buildOwner");

        Build build = new Build();
        build.setBuildId(buildId);
        build.setEstateId(estateId);
        build.setBuildName(name);
        build.setBuildAddress(buildAddress);
        build.setBuildFloornum(buildFloornum);
        build.setBuildUsearea(buildArea);
        build.setBuildUsetime(buildusertime);
        build.setBuildLasttime(buildlasttime);
        build.setBuildState(build_state);
        build.setBuildOpen(build_open);
        build.setBuildOwner(buildOwner);
        buildDao.insert(build);

        return "redirect:/success/addBuildManage_suc.jsp";
    }

    @RequestMapping("/openBuild")
    public String openEstate(HttpServletRequest request) {
        Long buildId = Long.valueOf(request.getParameter("build"));
        Build build = buildDao.selectById(buildId);
        build.setBuildOpen(1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("build_id", buildId);
        buildDao.update(build,wrapper);
        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/banBuild")
    public String banEstate(HttpServletRequest request) {
        Long buildId = Long.valueOf(request.getParameter("build"));
        Build build = buildDao.selectById(buildId);
        build.setBuildOpen(-1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("build_id", buildId);
        buildDao.update(build,wrapper);
        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/delBuild")
    public String delEstate(HttpServletRequest request) {
        Long buildId = Long.valueOf(request.getParameter("build"));
        buildDao.deleteById(buildId);
        return "redirect:/pro_page/p_estatemanage.jsp";
    }

}
